<?php

/*
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-05-08 20:18
 *
 * 项目：upload  -  $  - dzDBHelper.php
 *
 * 作者：liwei 
 */

namespace lev\helpers;

use Lev;
use lev\base\DBInterface;
use lev\db\DBV;

!defined('INLEV') && exit('Access Denied LEV');

class DBVHelper extends DBV implements DBInterface
{

    public static function isNameExist($username) {
        return UserLoginModelHelper::isNameExist($username);
    }

    public static function getAvatar($uid, $size = 'middle', $type = '') {
        return '';
    }
    public static function getUserInfo($uid = 0) {
        return !$uid ? [] : UserHelper::findOne(['id'=>$uid], 'id as uid,username,email,groupid,adminid');
    }

    public static function getUsers($uids = 0, $keys = [], $intval = 1, $unique = true) {
        $keys && is_array($uids) && $uids = Lev::getArrayColumn($uids, $keys, $intval, $unique);
        return UserLoginModelHelper::getUsers($uids);
    }

    public static function doLogin($username, $password, $autologin, $questionid = '', $answer = '', $ip = '') {
        return UserLoginModelHelper::login($username, $password);
    }

    public static function setLogin($userInfo, $ip = '', $cookietime = null) {
        return UserLoginModelHelper::setLoginStatus($userInfo['uid'], $userInfo['password'], $cookietime);
    }

    public static function doRegister($username, $password, $email, $questionid = '', $answer = '', $ip = '') {
        return UserLoginModelHelper::register($username, $password);
    }

    public static function onlyRegister($username, $password, $email, $questionid = '', $answer = '', $ip = '', $citycode = '') {
        return UserLoginModelHelper::register($username, $password, false, 0, 0, 0, $citycode);
    }

    public static function checkPasswordError($uid, $password) {
        return UserLoginModelHelper::checkPasswordError($uid, $password);
    }

    public static function setPassword($username, $password) {
        return UserLoginModelHelper::setPassword($username, $password);
    }

    public static function doLogout() {
        return UserLoginModelHelper::loginout(Lev::$app['uid']);
    }

    public static function myScores($pre = '') {
        return [];
    }

    /**
     * 用户积分使用 安装 levyy 仿抖音来赚钱模块
     * @param $spend
     * @param string $notice
     * @param int $type
     * @param int $uid
     * @param string $title
     * @param array $scoreArr
     * @return bool
     */
    public static function acscore($spend, $notice = '', $type = 0, $uid = 0, $title = '', $scoreArr = []) {
        return false;
    }

    /**
     * 获取用户积分
     * 用户积分 安装 levyy 仿抖音来赚钱模块
     * @param $uid
     * @return array
     */
    public static function getUserScore($uid) {
        return [];
    }

    public static function getGroups($field = '*') {
        return Lev::stget('groups', 'levs') ?: [];
    }

    public static function delete($table, $condition, $limit = 0, $unbuffered = true)
    {
        return parent::delete($table, dbHelper::setDataToCharset($condition), $limit, $unbuffered); // TODO: Change the autogenerated stub
    }

    public static function insert($table, $data, $return_insert_id = false, $replace = false, $silent = false)
    {
        return parent::insert($table, dbHelper::setDataToCharset($data), $return_insert_id, $replace, $silent); // TODO: Change the autogenerated stub
    }

    public static function update($table, $data, $condition = '', $unbuffered = false, $low_priority = false)
    {
        return parent::update($table, dbHelper::setDataToCharset($data), dbHelper::setDataToCharset($condition), $unbuffered, $low_priority); // TODO: Change the autogenerated stub
    }

    public static function fetch_all($sql, $arg = array(), $keyfield = '', $silent = false)
    {
        $data = parent::fetch_all(dbHelper::setDataToCharset($sql), $arg, $keyfield, $silent); // TODO: Change the autogenerated stub
        return dbHelper::getDataToCharset($data);
    }

    /**
     * @param $sql
     * @param array $arg
     * @param bool $silent
     * @return array|string
     */
    public static function fetch_first($sql, $arg = array(), $silent = false)
    {
        $data = parent::fetch_first(dbHelper::setDataToCharset($sql), $arg, $silent); // TODO: Change the autogenerated stub
        return dbHelper::getDataToCharset($data);
    }

}
